import { addAcross, delAcrosss } from '@/apis/acrossAPI'
import { addBet, delBets } from '@/apis/betAPI'
import { delOrders } from '@/apis/orderAPI'
import { addOther, delOthers } from '@/apis/otherAPI'
import {
  addSection,
  caclSection,
  calcTowerPointsSection,
  delSection,
  findAllSection,
  findSection,
  updateSection,
} from '@/apis/sectionAPI'
import { addTower, delTowers } from '@/apis/towerAPI'
import { addWire, delWires } from '@/apis/wireAPI'
import { addModels, readFromXlsx } from '@/common/readData'
import { acrossModel, betModel, otherModel, SectionDtoModel, SectionModel, towerModel, wireModel } from '@/types/model'
import { ref } from 'vue'

export default () => {
  const section = ref<SectionModel>()
  const sections = ref<SectionModel[]>([])

  const find = async (id: number) => {
    const { data } = await findSection(id)
    section.value = data
  }

  async function add(data: SectionDtoModel) {
    await addSection(data)
  }

  async function all(args = {}) {
    const { data } = await findAllSection(args)
    sections.value = data
  }

  async function del(id: number) {
    await delSection(id)
  }

  async function update(id: number, data: any) {
    await updateSection(id, data)
  }

  async function readData(sectionId: number) {
    await delModels(sectionId)
    await readModels(sectionId)
  }

  async function delModels(sectionId: number) {
    await delTowers(+sectionId)
    await delBets(+sectionId)
    await delAcrosss(+sectionId)
    await delWires(+sectionId)
    await delOthers(+sectionId)
    await delOrders(+sectionId)
  }

  async function readModels(sectionId: number) {
    await readFromXlsx(async (wb) => {
      const tower = {
        tName: '',
        tType: '',
        tStyle: '',
        corner: '',
        altitude: 1,
        remark: '',
        sectionId: 1,
      }
      await addModels<towerModel>(wb, sectionId, 'towers', tower, addTower)

      const bet = {
        btName: '',
        btSpan: 0,
        sectionId: 1,
      }
      await addModels<betModel>(wb, sectionId, 'bets', bet, addBet)

      const across = {
        btName: '',
        acrossName: '',
        acrossX: 0,
        acrossY: 0,
        controlHeight: 0,
        sectionId: 1,
      }
      await addModels<acrossModel>(wb, sectionId, 'across', across, addAcross)

      const wire = {
        wireName: '',
        wireStyle: '',
        wireWeight: 1,
        sectionId: 1,
      }
      await addModels<wireModel>(wb, sectionId, 'wires', wire, addWire)

      const other = {
        otherName: '',
        otherNum: 1,
        sectionId: 1,
      }
      await addModels<otherModel>(wb, sectionId, 'others', other, addOther)
    })
  }

  async function calcTowerPoints(args = {}) {
    await calcTowerPointsSection(args)
  }

  const cacl = async (args: Record<string, any>) => {
    await caclSection(args)
  }

  const calcCanvasAndHeight = async (offsetWidth: number, offsetHeight: number) => {
    const minXY = section.value?.minXY
    const maxXY = section.value?.maxXY

    const [minx, miny] = JSON.parse(minXY!)
    const [maxx, maxy] = JSON.parse(maxXY!)

    const startY = 2 * 10 * (Math.floor(miny / 2 / 10) - 1)
    let endY = 2 * 10 * (Math.floor(maxy / 2 / 10) + 4)
    const caclHeight = endY - startY

    const startX = 2 * 10 * Math.floor(minx / 2 / 10)
    let endX = 2 * 10 * (Math.ceil(maxx / 2 / 10) + 4)
    const caclWidth = endX - startX

    const margin = 40

    // 获取宽度和长度
    const width = Math.max(caclWidth + margin * 2, offsetWidth)
    const height = Math.max(caclHeight + margin * 2, offsetHeight)
    endX = 2 * 10 * Math.floor((width - margin * 2) / 2 / 10) + startX
    endY = 2 * 10 * Math.floor((height - margin * 2) / 2 / 10) + startY

    return {
      width,
      height,
      startX,
      endX,
      startY,
      endY,
    }
  }

  return { find, add, all, del, update, readData, cacl, calcCanvasAndHeight, calcTowerPoints, section, sections }
}
